<?php
include_once('/var/www/html/new/com.inc.php');
include_once('/var/www/html/new/d/class/Terminal_Check_Socket.class.php');

// http://www.12301.cc/api/terminal/terminal_order_search_v2.html?ac=123757&pw=828074ac9f9d648a2eb2992fc33aecb8&code=171412&sid=866268



if($_REQUEST['ac']=='')   exit('{"code":"1011","msg":"请输入授权账号"}');
if($_REQUEST['pw']=='')   exit('{"code":"1011","msg":"请输入授权密码"}');
if($_REQUEST['sid']=='')  exit('{"code":"1011","msg":"请输入景区ID"}');
if($_REQUEST['code']=='') exit('{"code":"1011","msg":"请输入凭证码"}');
// if(md5($_REQUEST['sid'].$_REQUEST['code'].'PFT12301')!=$_REQUEST['token']) exit('{"code":"1011","msg":"认证失败"}');


$salerid = $_REQUEST['sid'];  // 商户ID
$voucher = $_REQUEST['code']; // 凭证码


$ac      = $GLOBALS['_ac'];
$pw      = $GLOBALS['_pw'];
$inside  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$outSide = new SoapClient("http://112.124.14.164/open/openService/MXSE.wsdl",array('encoding' =>'UTF-8','cache_wsdl' => 0));
// $resxml = $outSide->__call('Order_Globle_Search', array($_REQUEST['ac'], $_REQUEST['pw'], 'salerid'=>$salerid, 'mid'=>'', 'aid'=>'6970', 'tid'=>'', 'ltitle'=>'','ttitle'=>'', '7'=>'2015-11-04', '8'=>'2015-11-05', '9'=>'', '10'=>'', '11'=>'', '12'=>'', 'ordernum'=>'', '14'=>'', 'ordername'=>'', 'ordertel'=>'', 'orderstatus'=>'', 'paystatus'=>1, '19'=>1, '20'=>1, '21'=>0, '22'=>10, '23'=>0, '24'=>'', '25'=>'', '26'=>'', '27'=>'', '28'=>0, '29'=>'', 'vcode'=>'', 'idcard'=>''));
// $simxml = simplexml_load_string($resxml);
// print_r($simxml);
// exit;

$option = array($_REQUEST['ac'], $_REQUEST['pw'], 'salerid'=>$salerid, 'mid'=>'', 'aid'=>'', 'tid'=>'', 'ltitle'=>'','ttitle'=>'', '7'=>'', '8'=>'', '9'=>'', '10'=>'', '11'=>'', '12'=>'', 'ordernum'=>'', '14'=>'', 'ordername'=>'', 'ordertel'=>'', 'orderstatus'=>'0', 'paystatus'=>1, '19'=>1, '20'=>1, '21'=>0, '22'=>'', '23'=>0, '24'=>'', '25'=>'', '26'=>'', '27'=>'', '28'=>0, '29'=>'', 'vcode'=>'', 'idcard'=>'');
if(strlen($voucher)==6) {
	$option['vcode'] = $voucher;
	$option['orderstatus'] = '0|1';
}
if(strlen($voucher)==11) $option['ordertel'] = $voucher;
if(strlen($voucher)!=11 && strlen($voucher)!=6) exit('[]');


$simxml = Get_Ticket_Lists($outSide, $option);
if(!isset($simxml->Rec)) exit('[]');
// print_r($simxml);
$send     = array();
$unique_a = array();// 多级转分校重复
foreach($simxml->Rec as $row){
	$ordernum = ((string)$row->UUconcat_id!=0) ? (string)$row->UUconcat_id:(string)$row->UUordernum;
	if(isset($unique_a[$ordernum])) continue;
	$unique_a[$ordernum] = $ordernum;	
	if((int)$row->UUifpack==2) $row->UUp_type = 'F';	
	$send = array_merge($send, Order_Search_Format($outSide, $option, $row));
}
// print_r($send);
exit(json_encode($send));


function Get_Ticket_Lists($outSide, $option){
	$resxml = $outSide->__call('Order_Globle_Search', $option);
	$simxml = simplexml_load_string($resxml);
	return $simxml;
}


function Order_Search_Format($outSide, $option, $order){
	
	
	
	$format_fun = function() use($order) {
		$returnData = array(); 
		$order_id = ((string)$order->UUconcat_id!=0) ? 
			(string)$order->UUconcat_id:(string)$order->UUordernum;
		$returnData[$order_id]['ordernum']  = $order_id;//  订单号
		$returnData[$order_id]['code']      = (string)$order->UUcode;
		$returnData[$order_id]['mcode']     = (string)$order->UUcode;
		$returnData[$order_id]['ptype']     = (string)$order->UUp_type;
		$returnData[$order_id]['pmode']     = (string)$order->UUpmode;
		$returnData[$order_id]['status']    = (string)$order->UUstatus;
		$returnData[$order_id]['landid']    = (string)$order->UUlid;
		$returnData[$order_id]['ordername'] = (string)$order->UUordername;// 取票人姓名
		$returnData[$order_id]['ordertel']  = (string)$order->UUordertel; // 取票人手机
		$returnData[$order_id]['ordertime'] = (string)$order->UUordertime;// 下单时间
		$returnData[$order_id]['begintime'] = (string)$order->UUbegintime;// 游玩时间
		$returnData[$order_id]['paystatus'] = (string)$order->UUpaystatus;// 支付状态
		$returnData[$order_id]['checktime'] = (string)$order->UUdtime;// 完成时间
		$returnData[$order_id]['endtime']   = (string)$order->UUendtime;// 完成时间
		$returnData[$order_id]['series']    = (string)$order->UUseries;// 完成时间
		return $returnData;
	};
	
	$ticket_fun = function($order){
		return array(
			'tnum'     => (int)$order->UUtnum,
			'name'     => (string)$order->UUttitle,
			'tprice'   => (string)$order->UUtprice,
			'ordernum' => (string)$order->UUordernum,
		);
	};
	
	
	// 联票再次获取
 	if((string)$order->UUconcat_id!=0){
		$option['vcode']    = '';
		$option['ordertel'] = '';
		$option['ordernum'] = (string)$order->UUconcat_id;
		$option[28] = 1;
		$resxml = $outSide->__soapCall('Order_Globle_Search', $option);
		$simxml = simplexml_load_string($resxml);
	}

	$returnData = array();
	if(isset($simxml->Rec)){// 联票
		// print_r($simxml);
		foreach($simxml->Rec as $order){
			$order_id = ((string)$order->UUconcat_id!=0) ? 
				(string)$order->UUconcat_id:(string)$order->UUordernum;			
			if(!isset($returnData[$order_id])) $returnData = $format_fun();
			$returnData[$order_id]['tickets'][] = $ticket_fun($order);
		}
	}else{
		// print_r($order);
		$order_id = (string)$order->UUordernum;
		if(!isset($returnData[$order_id])) $returnData = $format_fun();
		$returnData[$order_id]['tickets'][] = $ticket_fun($order);
	}
	return $returnData;	
}